using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TicTacToe.GC;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using TicTacToe.GamePlay;

namespace TicTacToe.GS
{
    public abstract class GameStateFactory<T> where T : class, new()
    {
        protected static T _instance = new T();
        public static T GetInstance()
        {
            return _instance;
        }

        protected bool isInit;
        protected TicTacToeGame gameMain;
        protected List<GameControl> gameControls;
        protected IGameState prevState;
        protected GameLogic gameLogic;

        protected GameStateFactory()
        {
            isInit = false;
            gameControls = new List<GameControl>();
        }

        public void Draw(SpriteBatch sprite, Color color)
        {
            foreach (GameControl gc in gameControls)
            {
                gc.Draw(sprite, color);
            }
        }

        public void Init()
        {
            foreach (GameControl gc in gameControls)
            {
                if (!gc.IsInit())
                {
                    gc.Init(gameMain.Content);
                }
            }
            isInit = true;
        }

        public void HandleTouch(TouchLocation touch)
        {
            foreach (GameControl gc in gameControls)
            {
                if (gc.IsContain((int)touch.Position.X, (int)touch.Position.Y))
                {
                    gc.OnPressed();
                    //gc.OnReleased();
                }
            }
        }

        public GameLogic GetGameLogic() { return gameLogic; }
        public bool IsInit() { return isInit; }
        public void ReInit() { isInit = false; }
        public void GoBack()
        {
            if (prevState != null)
                gameMain.SetGameState(prevState);
        }
        public void SetPreviousState(IGameState state) { prevState = state; }
        public void SetGameMain(TicTacToeGame game) { gameMain = game; }
        public TicTacToeGame GetGameMain() { return gameMain; }
    }
}
